use std::sync::{Arc, Mutex};

use diesel::{Connection, SqliteConnection};
use lazy_static::lazy_static;

use super::config::DATA_PATH;

lazy_static! {
	pub static ref DATABASE: Arc<Mutex<SqliteConnection>> = init_database();
}

pub fn init_database() -> Arc<Mutex<SqliteConnection>> {
	let mut data_path = DATA_PATH.lock().unwrap().clone();
	data_path.push_str("/server.db");
	let database_url = format!("file:{}", data_path);

	let connection = SqliteConnection::establish(&database_url).unwrap();
	Arc::new(Mutex::new(connection))
}
